<!DOCTYPE HTML>
<html>
<head>
    {include file="common:meta"}
</head>
<style>
    .count-txt{
        text-align: center;
        font-size: 15px;
        font-weight: bold;
        margin-top: 1%;
    }
</style>
<body>
<div class="page-container">
    <div class=" float-e-margins">
        <div class="ibox-content">
            <div class="tabs-container">

                <ul class="nav nav-tabs">
                    <li class="active"><a data-toggle="tab" href="#tab-1" aria-expanded="true">奖品列表</a>
                    </li>
                    <li class=""><a data-toggle="tab" href="#tab-2" aria-expanded="false">奖品添加</a>
                    </li>
                </ul>
                <div class="tab-content">
                    <div class=" tab-pane active" id="tab-1">
                        <div class="count-txt">
                            气球一追加轮次：<span id="gold_loop_18"></span>
                            气球二追加轮次：<span id="gold_loop_19"></span>
                            气球一当前轮次：<span id="current_gold_loop_18"></span>
                            气球二当前轮次：<span id="current_gold_loop_19"></span>
                        </div>
                        <div class="count-txt">
                            <!--状态：<span id='status'></span>-->
                            气球一默认库存：<span id="default_stock_18"></span>
                            气球二默认库存：<span id="default_stock_19"></span>
                            气球一剩余库存：<span id="current_stock_18"></span>
                            气球二剩余库存：<span id="current_stock_19"></span>
                        </div>
                        <div class="count-txt">
                            <!--<button class="change-type btn btn-w-m  btn-danger" >切换成该模式</button>-->
                            <button class="reset-loop btn btn-w-m btn-warning" >重置轮次为1</button>
                            <button class="edit-loop btn btn-w-m btn-warning" >添加轮次奖品</button>
                            <!--<button class="appoint-user btn btn-w-m btn-warning" >指定用户奖品</button>-->
                            <button class="clear-queue btn btn-w-m btn-warning" >清空队列</button>
                            <!--<button class="remain btn btn-w-m btn-warning" >当前轮次剩余奖品</button>-->
                            <button class="refresh btn btn-w-m btn-info" onclick="location.reload()" >刷新页面</button>
                        </div>
                        <table id="tb_departments"></table>
                    </div>
                    <div class=" tab-pane" id="tab-2">
                        <div class="ibox-content">
                            <form class="form-horizontal" id="form" data-url="/AgentIndexAdd">

                                <div class="form-group">
                                    <label class="col-sm-2 control-label" for="gold_egg_id">
                                        <span class="c-red">*</span>适用气球类型：
                                    </label>
                                    <div class="col-md-2 col-sm-10">
                                        <select class="form-control" name="gold_egg_id" id="gold_egg_id">
                                            <option value="18">气球一</option>
                                            <option value="19">气球二</option>
                                        </select>
                                    </div>
                                </div>


                                <div class="form-group">
                                    <label class="col-sm-2 control-label" for="gift_type">
                                        <span class="c-red">*</span>奖品类型：
                                    </label>
                                    <div class="col-md-2 col-sm-10">
                                        <select class="form-control" name="gift_type" id="gift_type">
                                            <option value="1">礼物</option>
                                            <option value="3">道具</option>
                                        </select>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-sm-2 control-label" for="gift_id">
                                        <span class="c-red">*</span>奖品名：
                                    </label>
                                    <div class="col-md-2 col-sm-10">
                                        <select class="form-control" name="gift_id" id="gift_id">
                                        </select>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-sm-2 control-label" for="default_stock">
                                        <span class="c-red">*</span>默认库存：
                                    </label>
                                    <div class="col-md-2 col-sm-10">
                                        <input type="number" min=0 class="form-control" id="default_stock"
                                               name="default_stock">
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-sm-2 control-label" for="gift_notice">
                                        <span class="c-red">*</span>提示语：
                                    </label>
                                    <div class="col-md-2 col-sm-10">
                                        <textarea name="gift_notice" id="gift_notice" cols="60" rows="10"></textarea>
                                    </div>
                                </div>

                                <input type="hidden" name="agent_type" value="4">

                                <div class="form-group">
                                    <div class="col-xs-8 col-sm-9 col-xs-offset-4 col-sm-offset-2">
                                        <button class="btn btn-primary" type="button" onclick="commonSubmit(this)">添加
                                        </button>
                                    </div>
                                </div>
                            </form>
                        </div>
                    </div>
                </div>

            </div>
        </div>
    </div>
</div>

<input type="hidden" id="agent_type" value="4">
{include file="common:footer"}
<script>
    $(document).ready(function () {
        initTable();
        getGiftInfo(4);
        getLoopStatus();
    });
    var agent_type  = $('#agent_type').val();

    function initTable() {
        $('#tb_departments').bootstrapTable({
            url: '/AgentIndexData',         //请求后台的URL（*）
            dataField: "data",
            method: 'get',                      //请求方式（*）
            striped: true,                      //是否显示行间隔色
            cache: true,                       //是否使用缓存，默认为true，所以一般情况下需要设置一下这个属性（*）
            pagination: true,                   //是否显示分页（*）
            queryParams: queryParams,//传递参数（*）
            showToggle: true,
            showRefresh: true,
            sidePagination: "server",           //分页方式：client客户端分页，server服务端分页（*）
            pageNumber: 1,                       //初始化加载第一页，默认第一页
            pageSize: 10,                       //每页的记录行数（*）
            pageList: [10, 25, 50, 100],        //可供选择的每页的行数（*）
            showColumns: true,                  //是否显示所有的列
            showPaginationSwitch: true,
            minimumCountColumns: 2,             //最少允许的列数

            columns: [{
                field: 'id',
                title: 'ID',
                sortable: true
            },{
                field: 'egg_name',
                title: '宝箱名称'
            }, {
                field: 'gift_name',
                title: '奖品名'
            }, {
                field: 'diamonds',
                title: '单价(钻)'
            }, {
                field: 'default_stock',
                title: '默认库存',
                editable: {
                    type: 'text',
                    title: '默认库存',
                    validate: function (v) {
                        if (!v) return '不能为空';
                    }
                }
            }, {
                field: 'gift_notice',
                title: '提示通知',
                editable: {
                    type: 'textarea',
                    title: '提示通知'
                }
            }, {
                field: 'create_time',
                title: '更新时间'
            }],
            //保存的使用
            onEditableSave: function (field, row, oldValue, $el) {
                //可进行异步操作
                $.ajax({
                    type: "get",
                    url: "/AgentIndexEdit",
                    data: row,
                    dataType: 'JSON',
                    success: function (re) {
                        if (re.code === 100) {
                            layer.msg(re.msg, {icon: 1, time: 2000});
                        } else {
                            layer.msg(re.msg,{icon: 2, time: 2000});
                        }
                    },
                    error: function () {
                        layer.alert('服务器开小差了',{icon: 2, time: 2000});
                    },
                    complete: function () {

                    }

                });
            }
        });
    }
    //得到查询的参数
    function queryParams(params) {
        params.offset = params.offset / params.limit + 1;
        params.agent_type = agent_type;
        console.log(JSON.stringify(params));
        return params;
    }

    function operateFormatter(value, row, index) {
        var prize_status = row.status === 1 ? '下架' : '上架';

        if(row.status === 1)  sho='btn-danger';
        else  sho='btn-primary';

//        return [
//            '<button type="button"  class="TableChange btn '+sho+' btn-primary btn-sm" style="margin-right:15px;">' + prize_status + '</button>'
//        ].join('');
    }

    window.operateEvents = {
        'click .TableChange': function (e, value, row, index) {
            changeStatus('GoldRoomPrizeChangePrizeStatus', row, this);
        }
    };

    function changeStatus(url, row, obj) {
        var prize_status = row.status === 1 ? '上架' : '下架';
        row.status === 1 ? row.status = 0 : row.status = 1;
        $.ajax({
            type: 'get',
            dataType: 'json',
            url: url,
            data: {'id': row.id},
            success: function (re) {
                if (re.code === 100) {
                    layer.msg(re.msg, {icon: 1, time: 2000});
                    $(obj).html(prize_status);
                } else {
                    layer.alert(re.msg);
                }
            },
            error: function (data) {
                console.log(data.msg);
            }
        });
    }

    //切换礼物
    $('#gift_type').change(function () {
        var gift_type = $('#gift_type').val();
        if(gift_type == 1) getGiftInfo(4);

        if(gift_type == 3) getGiftInfo(7);
    });
    function getGiftInfo(type)
    {
        $.ajax({
            type: 'get',
            dataType: 'json',
            url: '/GivePresenGiftName',
            data: {'prize_type': type},
            success: function (re) {
                if (re.code === 100) {

                    console.log(re);
                    var htmlText = "<option value='' selected>--请选择--</option>";
                    if(re.data !== null){

                        for(var i in re.data){

                            htmlText+="<option value="+ re.data[i]['id']+">"+ re.data[i]['name']+"</option>";
                        }
                        $('#gift_id').html(htmlText);
                    }
                }else{
                    layer.msg(re.msg,{
                        icon:2,
                        time:1500
                    });
                }
            },
            error: function (data) {
                layer.msg('服务器出小差了',{
                    icon:2,
                    time:1500
                });
            }
        });
    }


    //修改轮次  添加轮次奖品
    $('.edit-loop').click(function(){

        layer.confirm('选择气球', {
            btn: ['气球一','气球二','取消'] //按钮
        }, function(){
            openEditLoop(18);
        }, function(){
            openEditLoop(19);
        },function () {
            return false;
        });
    });
    //修改轮次
    function  openEditLoop(gold_id){
        layer.open({
            type: 2,
            title: ['修改轮次','font-size:18px;font-family:宋体;font-weight:700;'],
            shadeClose: true,
            shade: false,
            maxmin: true, //开启最大化最小化按钮
            area: ['800px', '600px'],
            content:"/AgentEditLoop?agent_type="+agent_type+'&gold_id='+gold_id,
            offset:'auto',
            scrollbar:false,
            end: function () {
                location.reload();
            }
        });
    }

    //清空队列
    $('.clear-queue').click(function () {
        layer.msg('友情提示:通知代理停止抽奖！');
        layer.confirm('选择清空的气球', {
            btn: ['气球一','气球二','取消'] //按钮
        }, function(){
            clearBoxQueue(18);
        }, function(){
            clearBoxQueue(19);
        },function () {
            return false;
        });
    });
    function clearBoxQueue(gold_id)
    {
        $.ajax({
            type: 'get',
            dataType: 'json',
            url: '/AgentClearBoxQueue',
            data: {'agent_type': agent_type,'gold_id':gold_id},
            success: function (re) {
                if (re.code === 100) {
                    layer.msg(re.msg, {icon: 1, time: 2000});
                } else {
                    layer.msg(re.msg, {icon: 2, time: 2000});
                }
            },
            error: function (data) {
                console.log(data.msg);
            }
        });
    }

    //重置轮数
    $('.reset-loop').click(function () {
        layer.confirm('选择要重置的气球', {
            btn: ['气球一','气球二','取消'] //按钮
        }, function(){
            resetLoop(18);
        }, function(){
            resetLoop(19);
        },function () {
            return false;
        });
    });
    //重置轮数
    function resetLoop(gold_id)
    {
        $.ajax({
            type: 'get',
            dataType: 'json',
            url: '/AgentResetLoop',
            data: {'agent_type': agent_type,'gold_id':gold_id},
            success: function (re) {
                if (re.code === 100) {
                    layer.msg(re.msg, {icon: 1, time: 2000});
                    location.reload();
                } else {
                    layer.msg(re.msg, {icon: 2, time: 2000});
                }
            },
            error: function (data) {
                console.log(data.msg);
            }
        });
    }

    //获取当前轮次状态
    function getLoopStatus() {
        $.ajax({
            type: 'get',
            dataType: 'json',
            url: '/AgentLoopStatus',
            data: {'agent_type': agent_type},
            success: function (re) {
//                $('#status').text(re.status);
                $('#gold_loop_18').text(re.gold_loop_18);
                $('#gold_loop_19').text(re.gold_loop_19);
                $('#current_gold_loop_18').text(re.current_gold_loop_18);
                $('#current_gold_loop_19').text(re.current_gold_loop_19);
                $('#default_stock_18').text(re.default_stock_18);
                $('#default_stock_19').text(re.default_stock_19);
                $('#current_stock_18').text(re.current_stock_18);
                $('#current_stock_19').text(re.current_stock_19);
            },
            error: function (data) {
                console.log(data.msg);
            }
        });

    }

    //切换模式
    $('.change-type').click(function () {
        $.ajax({
            type: 'get',
            dataType: 'json',
            url: '/ChangeType',
            data: {'mode_type': mode_type},
            success: function (re) {
                if (re.code === 100) {
                    layer.msg(re.msg, {icon: 1, time: 2000});
                    location.reload();
                } else {
                    layer.msg(re.msg, {icon: 2, time: 2000});
                }
            },
            error: function (data) {
                console.log(data.msg);
            }
        });
    });

    //查看剩余奖品
    $('.remain').click(function () {
        layer.confirm('选择宝箱', {
            btn: ['气球一','气球二','取消'] //按钮
        }, function(){
            remainBox(18);
        }, function(){
            remainBox(19);
        },function () {
            return false;
        });
    });

    function remainBox(gold_id)
    {
        layer.open({
            type: 2,
            title: ['当前轮次剩余奖品','font-size:18px;font-family:宋体;font-weight:700;'],
            shadeClose: true,
            shade: false,
            maxmin: true, //开启最大化最小化按钮
            area: ['800px', '600px'],
            content:"/RemainBox?mode_type="+mode_type+"&gold_id="+gold_id,
            offset:'auto',
            scrollbar:false
        });
    }

    //指定用户奖品
    $('.appoint-user').click(function () {
        layer.confirm('选择宝箱', {
            btn: ['气球一','气球二','取消'] //按钮
        }, function(){
            appointUser(18);
        }, function(){
            appointUser(19);
        },function () {
            return false;
        });
    });

    function appointUser(gold_id){
        layer.open({
            type: 2,
            title: ['指定用户奖品','font-size:18px;font-family:宋体;font-weight:700;'],
            shadeClose: true,
            shade: false,
            maxmin: true, //开启最大化最小化按钮
            area: ['800px', '600px'],
            content:"/AgentAppointUser?gold_id="+gold_id,
            offset:'auto',
            scrollbar:false
        });
    }

    //搜索按钮触发事件
    $(function () {
        $("#eventQuery").click(function () {
            $('#tb_departments').bootstrapTable(('refresh'));	// 很重要的一步，刷新url！
        });
    });


</script>
</body>
</html>